/*
 * @Author: Scott 
 * @Date: 2019-11-29 17:50:07 
 * @Last Modified by: Scott
 * @Last Modified time: 2019-11-30 00:54:27
 */
const fs         = require("fs");
  
const cheerio = require("cheerio");
const request = require('request');
const async = require("async");
const excelPort = require('excel-export');

request.cookie('MO_ID=5aa43ec181381b0735f2546c1f8940ab4424b6d9');

async function requestInfo(item, callback) {
  request({
    url: 'http://www.masuma.cn/' + item.link,
    method: 'get',
    headers: {
      'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36',
      'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3',
      // 'Accept-Encoding': 'gzip, deflate',
      'Accept-Language': 'zh-CN,zh;q=0.9',
      'Cache-Control': 'no-cache',
    },
    encoding: null
  }, (err, res, body) => {
      let codes = '';
      const $ = cheerio.load(body.toString());
      
      let htmlTr = $('.nysybm_tab tbody tr');
      console.log('zzzzzzzzzzzz', 'http://www.masuma.cn/' + item.link);
      // console.log('zzzzzzzzzzzz', body.toString());
      htmlTr.map((i, index) => {
        codes += `${$(index).find('.bm').text()} | `;
      });
      callback(null, {
        name: item.name,
        code: codes
      });
    }
  );
}

async function requestPage(page = 1, callback) {
  request({
    url: 'http://www.masuma.cn/product.php?lm=21&page=' + page,
    method: 'get',
    headers: {
      'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36',
      'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3',
      // 'Accept-Encoding': 'gzip, deflate',
      'Accept-Language': 'zh-CN,zh;q=0.9',
      'Cache-Control': 'no-cache',
    },
    encoding: null
  }, async (err, res, body) => {
      const list = [];
      const $ = cheerio.load(body.toString());
      
      let item = $('.nypro_tab tbody tr');
      item.map((i, index) => {
        let obj = {};
        obj.link = $(index).find('.bm').children('a').attr('href');
        obj.name = $(index).find('.bm').children('a').text();
        list.push(obj);
      });
      const codes = [];
      const requestInfoList = [];

      list.forEach((i) => {
        console.log(i.link);
        requestInfoList.push(infoCallback => {
          requestInfo(i, infoCallback);
        });
      });
      async.series(requestInfoList, (err, result) => {
        console.log('rereresult', result);
        callback(null, result);
      });
    }
  );
}

async function main() {
  const requestList = [];
  for (let i = 1; i < 36; i++) {
    requestList.push(callback => {
      requestPage(i, callback);
    });
  }
  console.log('requestListrequestListrequestList', requestList);
  async.series(requestList, (err, result) => {
    const arry = [].concat.apply([], result);
    console.log('最终结果!!!!', arry);
    console.log('最终结果条数!!!!', arry.length);
    writeExcel(arry);
  });
}

const writeExcel = (datas) => {
  //定义一个对象，存放内容
  let conf = {};
  //定义表头
  conf.cols = [
     {caption:'玛速玛编码', type:'string', width:40},
     {caption:'原厂编码', type:'string', width:60},
  ];
  //创建一个数组用来多次遍历行数据
  let array = [];
  // 循环导入从数据库中获取的表内容
  for (let i=0;i<datas.length;i++){
      //依次写入
    array[i] = [
      datas[i].name,
      datas[i].code,
    ];
  }
  //写入道conf对象中
  conf.rows = array;
  //生成表格
  const result = excelPort.execute(conf);
  // 定义表格存放路径
  fs.writeFile('./燃油滤.xlsx', result, 'binary',function(err){
      if(err){
          console.log(err);
      }
  });
}

main();